package io.github.common.util;

/**
 * 字符串工具及类方法
 *
 * @author haijun
 * @version 1.0.0
 * @email "1424132610@qq.com"
 * @date 2024.10.19 12:24
 * @since 1.0.0
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * 匹配 *
     *
     * @param basePackage base package
     * @param path        path
     * @return boolean boolean
     * @since 1.0.0
     */
    public static boolean match(String basePackage, String path) {
        boolean anyBlank = StringUtils.isAnyBlank(basePackage, path);
        if (anyBlank) {
            return false;
        }
        int basePackageLength = basePackage.length();
        int basePackagePos = 0;

        int pathLength = path.length();
        int pathPos = 0;

        while (pathPos < pathLength && basePackagePos < basePackageLength) {
            char bc = basePackage.charAt(basePackagePos);
            char pc = path.charAt(pathPos);
            if (bc == '*' && pc != '.') {
                pathPos++;
                continue;
            }
            //将后面 . 跳过
            if (bc == '*') {
                basePackagePos++;
                bc = basePackage.charAt(basePackagePos);
            }
            if (bc != pc) {
                break;
            }
            pathPos++;
            basePackagePos++;
        }
        return basePackagePos == basePackageLength && pathPos == pathLength;
    }

    /**
     * 去掉空格
     *
     * @param str str
     * @return string
     * @since 1.0.0
     */
    public static String trimWhitespace(String str) {
        if (isEmpty(str)) {
            return str;
        } else {
            int beginIndex = 0;
            int endIndex;
            for (endIndex = str.length() - 1; beginIndex <= endIndex && Character.isWhitespace(str.charAt(beginIndex)); ++beginIndex) {
            }
            while (endIndex > beginIndex && Character.isWhitespace(str.charAt(endIndex))) {
                --endIndex;
            }
            return str.substring(beginIndex, endIndex + 1);
        }
    }

    /**
     * 去掉所有空格
     *
     * @param str str
     * @return string
     * @since 1.0.0
     */
    public static String trimAllWhitespace(String str) {
        if (isEmpty(str)) {
            return str;
        } else {
            int len = str.length();
            StringBuilder sb = new StringBuilder(str.length());
            for(int i = 0; i < len; ++i) {
                char c = str.charAt(i);
                if (!Character.isWhitespace(c)) {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
    }

    /**
     * 截取符号出现指定次数之前的字符串
     *
     * @param input      字符串
     * @param symbol     符号
     * @param occurrence 出现的次数
     * @return string
     * @since 1.0.0
     */
    public static String subStringBeforeNthOccurrence(String input, char symbol, int occurrence) {
        int symbolCount = 0;
        int index = -1;
        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) == symbol) {
                symbolCount++;
                if (symbolCount == occurrence) {
                    index = i;
                    break;
                }
            }
        }
        if (index != -1) {
            return input.substring(0, index);
        }
        return input;
    }
}
